package com.acme.bankapp.domain.account;

import com.acme.bankapp.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
    static private int counter = 0;

    private final int accountID = counter++;

    protected double balance;

    protected AbstractAccount(final double startBalance) {
	super();
	if (balance < 0) {
	    throw new IllegalArgumentException();
	}
	balance = startBalance;
    }

    @Override
    public long decimalValue() {
	return Math.round(balance);

    }

    @Override
    public void deposit(final double x) {
	balance += x;
    }

    @Override
    public boolean equals(final Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	AbstractAccount other = (AbstractAccount) obj;
	if (accountID != other.accountID) {
	    return false;
	}
	if (balance != other.balance) {
	    return false;
	}
	return true;
    }

    @Override
    public double getBalance() {
	return balance;
    }

    @Override
    public int hashCode() {
	int result = accountID;
	return result;
    }

    @Override
    public double maximumAmountToWithdraw() {
	return balance;
    }

    @Override
    public String toString() {
	return "Account's balance = " + getBalance();
    }

    @Override
    public void withdraw(final double x) throws NotEnoughFundsException {
	if (maximumAmountToWithdraw() <= x) {
	    throw new NotEnoughFundsException("Not enough funds!");
	}
	if (balance >= x) {
	    balance -= x;
	}
    }
}
